﻿// JavaScript Document

function slider_image(container, target) {
	this.container = $E(container);
	this.target = $E(target);
	this.imgs = [];
	this.imgsd = [];
	this.eids = [];
	this.layer = null;
	this.width = 110;
	this.popup = false;
	this.c = 0;
	//
	this.init = function(){
		var imgs = this.container.getElementsByTagName("IMG");
		for(var i=0; i<imgs.length; i++) {
			//var img = new Image();
			//img.src = imgs[i].src.replace('thumbs', 'resized');
			//var eid = dgetAttr(imgs[i], "eid");
			//this.imgs[eid] = img;
			var eid = dgetAttr(imgs[i], "eid");
			this.eids.push(eid);
			this.imgs[eid] = imgs[i].src.replace('thumbs', 'resized');
			this.imgsd[eid] = null;
		}
		this.length = imgs.length;
		//
		if( this.popup == true ) {
			this.dlayer();
		}
		//
		//var t = Math.floor(this.length%10);
		//var c = '';
		//for(var i=0; i+1<t; i++) {
		//	c += '<a href="javascript:;" onclick="">'+(i*10+1)+'</a> ';
		//}
		//$E("gnav").innerHTML = c;
	};
	this.start = function(){
		
	};
	this.effect = function(){
		
	};
	this.click = function(eid) {
		this.c = eid;
		//
		if( this.popup == true && this.layer != null ) {
			dmask(40);
			this.show();
			this.layer_content.innerHTML = "<img src=\""+this.imgs[eid]+"\">";
			dmaskd();
		}
		if( this.target == null ) {
			//
		} else {
			//this.target.src = this.imgs[eid].src;
			//this.target.src = this.imgs[eid];
			var _obj = this;
			if( _obj._timer ) {
				window.clearInterval(_obj._timer);
			}
			_obj._timer = null;
			//
			if( this.imgsd[eid] != null ) {
				this.target.src = this.imgsd[eid].src;
			} else {
				var img = new Image();
				img.src = this.imgs[eid];
				img.onload = function(){
					_obj.target.src = _obj.imgs[eid];
					if( _obj.target.parentNode.tagName.toUpperCase() == "A" ) {
						//_obj.target.parentNode.href = _obj.target.src.replace('resized', 'origins');
					}
					//var psc = _obj._pscount(eid);
					//$E("gtxt").innerHTML = (psc+1) + " of " + _obj.length;
					//var t = Math.max(0, (psc-3) * _obj.width);
					//_obj.container.scrollLeft = t;
					//_obj._move(_obj.container.scrollLeft, t);
				};
			}
			var psc = _obj._pscount(eid);
			$E("gtxt").innerHTML = (psc+1) + " of " + _obj.length;
			var t = Math.max(0, (psc-2) * _obj.width);
			_obj._move(_obj.container.scrollLeft, t);
			
			//
			this._preload(eid);
		}
	};
	this.select = function(eid){
		document.location.hash = eid;
		this.click(eid);
	};
	this.current = function(){
		var eid = parseInt(document.location.hash.substring(1));
		if( eid > 0 ){
			this.select(eid);
		}
	};
	this.previous = function(){
		if( this.c > 0 ){
			var eid = this._psprevious(this.c);
			this.select(eid);
		}
	};
	this.next = function(){
		if( this.c > 0 ){
			var eid = this._psnext(this.c);
			this.select(eid);
		}
	};
	this.show = function(){
		if( this.popup == true && this.layer != null ) {
			this.layer.style.display = "";
		}
	};
	this.hide = function(){
		if( this.popup == true && this.layer != null ) {
			this.layer.style.display = "none";
		}
	};
	//
	this._preload = function(eid){
		for(var i=0; i<this.eids.length; i++) {
			if( this.eids[i] == eid ) {
				for(var j=Math.max(0, i-4); j<Math.min(i+4, this.eids.length); j++){
					var _eid = this.eids[j];
					if( this.imgsd[_eid] != null ) {
						continue;	
					}
					var pimg = new Image();
					pimg.eid = _eid;
					pimg.src = this.imgs[_eid];
					var _obj = this;
					pimg.onload = function(){
						_obj.imgsd[this.eid] = this;
					};
				}
				break;
			}
		}
	};
	this._pscount = function(eid) {
		var c = 0;
		for(var i=0; i<this.eids.length; i++) {
			if( this.eids[i] == eid ) {
				break;
			}
			c++;
		}
		return c;
	};
	this._psprevious = function(eid, s) {
		var peid = eid;
		if( !s ) s = 1;
		for(var i=0; i<this.eids.length; i++) {
			if( this.eids[i] == eid ) {
				if( i-s >= 0 ) {
					peid = this.eids[i-s];
					break;
				} else {
					peid = this.eids[0];
					break;
				}
			}
		}
		return peid;
	};
	this._psnext = function(eid,s) {
		var neid = eid;
		if( !s ) s = 1;
		for(var i=0; i<this.eids.length; i++) {
			if( this.eids[i] == eid ) {
				if( i+s < this.eids.length ) {
					neid = this.eids[i+s];
					break;
				} else {
					neid = this.eids[this.eids.length-1];
					break;
				}
			}
		}
		return neid;
	};
	this._move = function(c, t) {
		var _obj = this;
		if( t >= _obj.container.scrollWidth - _obj.container.offsetWidth ) {
			_obj.container.scrollLeft = _obj.container.scrollWidth - _obj.container.offsetWidth;
			if( _obj._timer ) {
				window.clearInterval(_obj._timer);
			}
			_obj._timer = null;
			return true;
		} else if( t <= 0 ) {
			_obj.container.scrollLeft = 0;
			if( _obj._timer ) {
				window.clearInterval(_obj._timer);
			}
			_obj._timer = null;
			return true;
		}
		var s = 10;
		if( Math.abs(c-t) > 200 ) {
			s = 20;
		}
		if( _obj.container.scrollLeft > t + s ) {
			_obj.container.scrollLeft -= s;
		} else if( _obj.container.scrollLeft < t - s ) {
			_obj.container.scrollLeft += s;
		} else {
			_obj.container.scrollLeft = t;
			if( _obj._timer ) {
				window.clearInterval(_obj._timer);
			}
			_obj._timer = null;
			return true;
		}
		if( _obj._timer == null ) {
			_obj._timer = window.setInterval(function(){_obj._move(c, t);}, 10);
		}
	};
	this.dlayer = function(){
		var element = document.body;
		var objBrother = element.childNodes[0];
		
		//var width = 600;
		//var height = 540 + getScrollY();
		var left = getOffsetLeft($E("main")) + 150;
		var top = getOffsetTop($E("main")) + 20;
		
		var divObj = document.createElement('DIV');
		divObj.setAttribute("id", "slider");
		//divObj.setAttribute("class", "slider");
		divObj.className = "slider";
		divObj.style.textAlign = "center";
		divObj.style.position = "absolute";
		divObj.style.zIndex = "200";
		divObj.style.left = left + "px";
		divObj.style.top = top + "px";
		//divObj.style.width = width + "px";
		//divObj.style.height = height + "px";
		//divObj.style.margin = "auto";
		//divObj.style.padding = "0px";
		//divObj.style.backgroundColor = "#000000";
		
		element.insertBefore(divObj, objBrother);
		
		//
		this.layer = divObj;
		this.layer_top = appendDIV(this.layer, "slider_top", "stop", "");
		this.layer_close = appendDIV(this.layer_top, "slider_close", "sclose", "");
		this.layer_main = appendDIV(this.layer, "slider_main", "smain", "");
		this.layer_left = appendDIV(this.layer_main, "slider_left", "sleft", "");
		this.layer_right = appendDIV(this.layer_main, "slider_right", "sright", "");
		this.layer_content = appendDIV(this.layer_main, "slider_content", "scontent", "");
		this.layer_text = appendDIV(this.layer_main, "slider_text", "stext", "");
		this.layer_bottom = appendDIV(this.layer, "slider_bottom", "sbottom", "");
		//
		this.layer_top.style.position = "";
		this.layer_close.style.position = "";
		this.layer_main.style.position = "";
		this.layer_left.style.position = "";
		this.layer_right.style.position = "";
		this.layer_content.style.position = "";
		this.layer_text.style.position = "";
		this.layer_bottom.style.position = "";
		//
		//this.layer.innerHTML = '<div class="rct"><div class="rc1"></div><div class="rc2"></div><div class="rc3"></div><div class="rc4"></div></div>' + this.layer.innerHTML;
		//this.layer.innerHTML += '<div class="rcb"><div class="rc4"></div><div class="rc3"></div><div class="rc2"></div><div class="rc1"></div></div>';
		//
		this.layer_close.innerHTML = 'X';
		this.layer_left.innerHTML = "&lsaquo;";
		this.layer_right.innerHTML = "&rsaquo;";
		this.layer_text.innerHTML = '<div id="gtxt"></div>';
		//
		var _obj = this;
		this.layer_close.onclick = function(){_obj.hide();};
		this.layer_left.onclick = function(){_obj.previous();};
		this.layer_right.onclick = function(){_obj.next();};
		this.layer_content.onclick = function(){_obj.next();};
		//
		this.layer.style.display = "none";
	};
	//
	if( !$E(container) ) {
		return false;
	}
	//
	//this.init();
}

//
function slider_hs(tc) {
	this.timer = null;
	this.ts = 40;
	this.da = null;
	this.db = null;
	this.s = 0;
	this.init = function(tc) {
		this.da = $E(tc);
		this.db = appendDIV(this.da.parentNode, "", "", this.da.innerHTML);
	};
	this.start = function(){
		
	};
	//
	this.init(tc);
}